<?php

App::uses('AppModel', 'Model');

class Credito extends AppModel
{
    var $useTable = 'creditos';

    public function getCreditosVigentesByRfc($rfc)
    {
        $prestamos = $this->query("
            SELECT *
            FROM creditos
            WHERE 
                    rfc LIKE ? 
                AND liquidado = 0
            ORDER BY fecha_instrumento ASC
            ", array(
            $rfc));
        return $prestamos;
    }

    public function getByRfc($rfc)
    {
        $prestamos = $this->query("
            SELECT *
            FROM creditos
            WHERE 
                rfc LIKE ? 
            ORDER BY fecha_instrumento ASC
            ", array(
            $rfc));
        return $prestamos;
    }

    public function liquidar($id)
    {
        $prestamos = $this->query("
            UPDATE creditos
            SET liquidado = 1
            WHERE id = ?
            ", array(
            $id));
    }

    public function insert(array $post)
    {
        $dt = new DateTime();
        $data = array();
        $data['rfc'] = $post['rfc'];
        $data['folio'] = $post['folio'];
        $data['delegacion'] = $post['delegacion'];
        $data['nombre'] = $post['nombre'];
        $data['paterno'] = $post['paterno'];
        $data['materno'] = $post['materno'];
        $data['cuenta_bancaria'] = $post['cuenta'];
        $data['monto_original'] = $post['monto'];
        $data['plazo'] = $post['plazo'];
        $data['pago_mensual'] = $post['pagoMensual'];
        $data['total_pago'] = $post['totalPago'];
        $data['ahorro'] = $post['ahorro'];
        $data['interes_mensual'] = $post['interes'];
        $data['instrumento'] = $post['instrumento'];
        $data['fecha_instrumento'] = $post['fechaInstrumento'];
        $data['migrado'] = 0; #No migrado
        $data['liquidado'] = 0; #Sin liquidar
        $data['fecha_creacion'] = $dt->format('Y-m-d H:i:s');
        $this->save($data);
    }

}
